package com.hnevc.zz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hnevc.zz.R;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern;



public class RegistActivity extends AppCompatActivity {
    private EditText et_name,et_pass,et_repass,et_phone;
    private Button submit,cancle,back;
    private String username,pass,repass,phone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        et_name= (EditText) findViewById(R.id.et_name_re);
        et_pass= (EditText) findViewById(R.id.et_pass_re);
        et_repass= (EditText) findViewById(R.id.et_rename_re);
        et_phone= (EditText) findViewById(R.id.et_phone_re);
        submit= (Button) findViewById(R.id.btn_submit_re);
        cancle= (Button) findViewById(R.id.btn_cancel_re);
        username=et_name.getText().toString();
        pass=et_pass.getText().toString();
        repass=et_repass.getText().toString();
        phone=et_phone.getText().toString();
        back=findViewById(R.id.btn_back_re);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RegistActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RegistActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
        String regex_name="^[A-Za-z0-9]${4,10}";
        if (Pattern.matches(regex_name,username)==false){
            Toast.makeText(RegistActivity.this,"用户名输入不合法",Toast.LENGTH_SHORT).show();
        }
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username=et_name.getText().toString();
                pass=et_pass.getText().toString();
                repass=et_repass.getText().toString();
                phone=et_phone.getText().toString();

                if (TextUtils.isEmpty(username)||TextUtils.isEmpty(pass)||TextUtils.isEmpty(phone)||pass!=repass){
                    String regex_name="^[A-Za-z0-9]${4,10}";
                    if (Pattern.matches(regex_name,username)==false){
                        Toast.makeText(RegistActivity.this,"用户名输入不合法",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (pass!=repass){
                        Toast.makeText(RegistActivity.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
                        return;
                    }

                    Toast.makeText(RegistActivity.this,"信息输入有误",Toast.LENGTH_SHORT).show();
                    return;
                }
                Timer timer=new Timer();
                    TimerTask timerTask=new TimerTask() {
                        @Override
                        public void run() {
                            Intent intent=new Intent(RegistActivity.this, MainActivity.class);
                            startActivity(intent);
                            Toast.makeText(RegistActivity.this,"注册成功，即将跳转到主页面",Toast.LENGTH_SHORT).show();
                        }
                    };
                    timer.schedule(timerTask,3000);
                finish();
            }
        });






    }
}
